package com.tuanzi.loan.business.analyzer;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Maps;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.tuanzi.loan.business.service.system.DictionaryService;
import com.tuanzi.loan.core.util.GsonUtils;
import com.tuanzi.loan.vo.view.family.UserValidateView;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Ray 2017/11/01
 */
@Slf4j
@Component
public class DianJinAnalyzer {

	@Autowired
	private DictionaryService dictionaryService;

	private Map<String /* value */, String /* name */> dictionaries = Maps.newHashMap();

	@PostConstruct
	private void init() {
		dictionaries = dictionaryService.listMapByCode("DIAN_JIN_TYPE");
	}

	/**
	 * 根据点金征信JSON构造VO对象
	 *
	 * @param json
	 * @return UserValidateView.DianJin
	 */
	public UserValidateView.DianJin analyze(String json) {
		if (StringUtils.isBlank(json)) {
			return null;
		}
		try {
			UserValidateView.DianJin dianJin = new UserValidateView.DianJin();

			JsonObject root = new JsonParser().parse(json).getAsJsonObject();
			String province = GsonUtils.getAsString(root, "PROVINCE");
			String city = GsonUtils.getAsString(root, "CITY");

			dianJin.setAddress(province + " " + city);

			List<UserValidateView.DianJinResult> results = StreamSupport.stream(root.get("RESULTS").getAsJsonArray().spliterator(), false).map(x -> x.getAsJsonObject()).map(x -> {

				return StreamSupport.stream(x.get("DATA").getAsJsonArray().spliterator(), false).map(y -> y.getAsJsonObject()).map(y -> {
					UserValidateView.DianJinResult result = new UserValidateView.DianJinResult();
					result.setOriginalType(GsonUtils.getAsString(x, "TYPE"));
					result.setType(dictionaries.get(result.getOriginalType()));
					result.setCycle(GsonUtils.getAsString(x, "CYCLE"));
					result.setPlatformType(GsonUtils.getAsString(y, "P_TYPE") == "1" ? "银行" : "非银行");
					result.setPlatformCode(GsonUtils.getAsString(y, "PLATFORMCODE"));
					result.setRegisterTime(GsonUtils.getAsString(y, "REGISTERTIME"));
					result.setApplicationTime(GsonUtils.getAsString(y, "APPLICATIONTIME"));
					result.setApplicationAmount(GsonUtils.getAsString(y, "APPLICATIONAMOUNT"));
					result.setApplicationResult(GsonUtils.getAsString(y, "APPLICATIONRESULT"));
					result.setLoanLendersTime(GsonUtils.getAsString(y, "LOANLENDERSTIME"));
					result.setLoanLendersAmount(GsonUtils.getAsString(y, "LOANLENDERSAMOUNT"));
					result.setCounts(GsonUtils.getAsString(y, "COUNTS"));
					result.setMoney(GsonUtils.getAsString(y, "MONEY"));
					return result;
				}).collect(Collectors.toList());

			}).flatMap(x -> x.stream()).collect(Collectors.toList());

			dianJin.setResults(results);
			return dianJin;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}
}
